<template>
  <div class="mod-seckill-shopAuditing">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="searchForm"
      >
        <div class="input-row margin-bottom-none">
          <el-form-item
            prop="shopName"
            :label="$t('prodList.shopName') + ':'"
          >
            <el-input
              v-model="searchForm.shopName"
              clearable
              :placeholder="$t('prodList.shopName')"
            />
          </el-form-item>
          <el-form-item
            prop="intro"
            :label="$t('shop.shopProfile') + ':'"
          >
            <el-input
              v-model="searchForm.intro"
              clearable
              :placeholder="$t('shop.shopProfile')"
            />
          </el-form-item>
          <el-form-item
            :label="$t('order.applicationTime') + ':'"
          >
            <el-date-picker
              v-model="dateRange"
              type="datetimerange"
              :default-time="[new Date(2000, 1, 1, 0, 0, 0),
                              new Date(2000, 2, 1, 23, 59, 59)]"
              :range-separator="$t('date.tip')"
              value-format="YYYY-MM-DD HH:mm:ss"
              :start-placeholder="$t('date.start')"
              :end-placeholder="$t('date.end')"
            />
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('product.search') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch()"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="table-con audit-shop-table">
        <el-table
          :key="tabValue"
          ref="auditShopRef"
          :data="dataList"
          header-cell-class-name="table-header"
          style="width: 100%"
        >
          <el-table-column
            prop="shopName"
            :label="$t('prodList.shopName')"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.shopName || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="shopLogo"
            :label="$t('shop.shopLogo')"
          >
            <template #default="scope">
              <div class="table-cell-image">
                <img-show :src="scope.row.shopLogo" />
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="intro"
            :label="$t('shop.shopProfile')"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.intro || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="shopStatusList"
            :label="$t('shop.storeStatus')"
          >
            <template #default="scope">
              <span
                v-if="scope.row.status === -1"
                class="tag-text"
              >{{ $t('shop.failed') }}</span>
              <span
                v-else-if="scope.row.status === 0"
                class="tag-text"
              >{{ $t('shop.unreviewed') }}</span>
              <span
                v-else-if="scope.row.shopStatus === 1"
                class="tag-text"
              >{{ $t('shop.passed') }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            :label="$t('order.applicationTime')"
          >
            <template #default="scope">
              <span class="table-cell-text">{{ scope.row.createTime || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('publics.operating')"
            width="150px"
          >
            <template #default="scope">
              <div
                v-if="isAuth('shop:shopAuditing:audit')"
                class="default-btn text-btn"
                @click="shopAuditHandle(scope.row.shopId)"
              >
                {{ $t('productComm.audit') }}
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>

    <!-- 账号管理 -->
    <ShopAccount
      v-if="shopAccountVisible"
      ref="shopAccountRef"
      @refresh-data-list="refreshChange"
    />
  </div>
</template>

<script setup>
import ShopAccount from '../shop-account/index.vue'
import { isAuth } from '@/utils/index.js'

const router = useRouter()

defineProps({
  tabValue: {
    type: String,
    default: ''
  }
})

let theParams = null // 保存上次点击查询的请求条件
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = reactive({
  shopStatusList: undefined,
  intro: undefined,
  shopName: undefined
}) // 搜索
const shopAccountVisible = ref(false)

let theData = null // 保存上次点击查询的请求条件
const dateRange = ref([])
const dataList = ref([])

onMounted(() => {
  getDataList()
})

const getDataList = (pageParam, newData = false) => {
  pageParam = pageParam || page
  if (newData || !theData) {
    theData = {
      current: pageParam.currentPage,
      size: pageParam.pageSize,
      beginTime: dateRange.value ? dateRange.value[0] : null, // 开始时间
      endTime: dateRange.value ? dateRange.value[1] : null // 结束时间
    }
    theParams = JSON.parse(JSON.stringify(searchForm))
    theParams.shopStatusList = [5]
    theParams.isChooseApply = 1
  } else {
    theData.current = pageParam.currentPage
    theData.size = pageParam.pageSize
  }
  http({
    url: http.adornUrl('/shop/shopAuditing/page'),
    method: 'get',
    params: http.adornParams(Object.assign(theData, theParams))
  }).then(({ data }) => {
    dataList.value = data.records
    page.total = data.total
    page.currentPage = data.current
  })
}

/**
 * 刷新回调
 */
const refreshChange = () => {
  getDataList(page)
}

const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}

// 弹窗处理
const shopAuditHandle = (id) => {
  router.push({
    path: '/platform/audit-shop/detail/index',
    query: {
      shopId: id,
      pageType: 'toAudit'
    }
  })
}

const searchFormRef = ref(null)
/**
 * 重置表单
 */
const onResetSearch = () => {
  searchFormRef.value.resetFields()
  searchForm.info = undefined
  dateRange.value = []
}

const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList()
}

const onPageChange = (val) => {
  page.currentPage = val
  getDataList()
}

const init = () => {
  onResetSearch()
  onSearch(true)
}

defineExpose({
  init
})

</script>
